'use strict';

const db = uniCloud.database()
const dbCmd = db.command

exports.main = async (event, context) => {
	const{
		user_id,
		article_id
	} = event
	
	const userinfo = await db.collection('user').doc(user_id).get()
	
	// console.log(userinfo)
	
	const article_id_ids = userinfo.data[0].article_likes_ids
	console.log(article_id_ids)
	let dbComFuns = null
	
	if(article_id_ids.includes(article_id)){
		dbComFuns = dbCmd.pull(article_id)
	}else{
		dbComFuns = dbCmd.addToSet(article_id)
	}
	
	await db.collection('user').doc(user_id).update({
		article_likes_ids: dbComFuns
	})
	
	//返回数据给客户端
	return {
		code: 200,
		msg: "数据请求成功",
		data: userinfo.data[0]
	}
};
